package tk.mybatis.springboot.common;


import cn.hutool.json.JSONUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;

public class TaskUtil {

        public static final String PREV_URL = "http://192.168.127.40:8900/";
        public static CloseableHttpResponse httpPost(String url,Object param) throws IOException {
// 创建 CloseableHttpClient
            CloseableHttpClient httpClient = createHttpClient();

            // 创建 HttpPost 请求
            HttpPost httpPost = new HttpPost(url);

            // 设置请求头 Content-Type 为 application/json
            httpPost.setHeader("Content-Type", "application/json");

            StringEntity stringEntity = new StringEntity(JSONUtil.toJsonStr(param), ContentType.APPLICATION_JSON);
            httpPost.setEntity(stringEntity);

            // 执行请求
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

            return httpResponse;
        }

    public static CloseableHttpClient createHttpClient() {
        // 创建 HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        // 设置配置（可选）

        // 构建 CloseableHttpClient
        return httpClientBuilder.build();
    }

}
